package com.mc.dao.Impl;
import com.mc.bean.MatchType;
import com.mc.dao.MatchTypeDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 赛事类型数据访问实现类
 * @author fyp
 */
public class MatchTypeDaoImpl extends BaseDao implements MatchTypeDao {

    @Override
    public List<MatchType> findAll() {
        //创建集合对象
        List<MatchType> list=new ArrayList<>();
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql="select*from matchType";
            super.getPst(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                //创建对象
                MatchType matchType=new MatchType(rs.getInt(1),rs.getString(2));
                //将对象放入集合中
                list.add(matchType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return list;
    }

    @Override
    public MatchType findByMTid(int mtid) {
        MatchType matchType=null;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "select*from matchType where mtid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mtid);
            //执行预编译指令，得到结果
            rs=pstmt.executeQuery();
            while (rs.next()){
                matchType=new MatchType(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return matchType;
    }

    @Override
    public int add(MatchType matchType) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "insert into matchType(mtid,mtname) values (?,?)";
            super.getPst(sql);
            super.pstmt.setInt(1,matchType.getMtid());
            super.pstmt.setString(2,matchType.getMtname());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int delete(int mtid) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql="delete from matchType where mtid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mtid);
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int update(MatchType matchType) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "update MatchType set mtname=? where mtid=?";
            super.getPst(sql);
            super.pstmt.setString(1,matchType.getMtname());
            super.pstmt.setInt(2,matchType.getMtid());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }
}
